 <template>
  <div style="height: 100%; background-color: white">

    <el-row>
      <div>
        <el-button type="primary" size="small" @click="openAddDialoge()">新增</el-button>
        <el-input placeholder="请输入图书名称或ISBN或选题号" size="small" style="width:260px" v-model="searchText"></el-input>
        <el-select v-model="sourcePubType" size="small" style="width:120px" placeholder="请选择类型">
          <el-option key="0" label="全部" value=0></el-option>
          <el-option key="1" label="加密" value=1></el-option>
          <el-option key="2" label="开放" value=2></el-option>
          <el-option key="3" label="会员" value=3></el-option>
        </el-select>
        <el-select v-model="pubStatus" size="small" style="width:140px" placeholder="请选择发布状态">
          <el-option key="0" label="全部" value=0></el-option>
          <el-option key="1" label="新建" value=1></el-option>
          <el-option key="2" label="发布" value=2></el-option>
        </el-select>
        <el-button type="primary" size="small" @click="getTableData()">搜索</el-button>
        <el-button style="margin-left:2px" type="warning" size="small" @click="resetSearch()">重置</el-button>
      </div>
    </el-row>

    <el-row>
      <div>
        <el-table :data="tableData" :height="tableHeight" @selection-change="handleSelectionChange" style="width: 100%;margin-top:10px">
          <el-table-column type="selection" align="center" v-model="multipleSelection" width="45"></el-table-column>
          <el-table-column prop="bookName" align="center" label="图书名称" ></el-table-column>
          <el-table-column prop="isbn" align="center" label="ISBN" :formatter="formatterIsbn" ></el-table-column>
          <el-table-column prop="topicNumber" align="center" label="选题号"  ></el-table-column>
          <el-table-column prop="sourcePubType" align="center" label="类型"  width="60" :formatter="formatterPubType" ></el-table-column>
          <el-table-column prop="createBy" align="center" label="创建人" width="120"></el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间" width="140" :formatter="formatterTime"> </el-table-column>
          <el-table-column prop="pubStatus" align="center" label="发布状态" width="100" :formatter="formatterStatus"> </el-table-column>
          <el-table-column prop="pubTime" align="center" label="发布时间" width="140" :formatter="formatterTime"> </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="scope">
              <div>
                <div v-if="scope.row.pubStatus==1" style="float:left">
                  <el-button type="primary" size="small" @click="generateQrBook(scope.row)">付型</el-button>
                </div>
                <div style="float:left;margin-top:5px;">
                  <el-button type="primary" size="small" @click="openEditDialoge(scope.row)">编辑</el-button>
                  <el-button style="margin-left:3px" v-if="scope.row.pubStatus==1" type="danger" size="small" @click="deleteData(scope.row)">删除</el-button>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-row>

    <el-row>
      <div style="margin-top:10px;">
        <el-pagination
          background
          :hide-on-single-page="false"
          :total="totalNum"
          :page-size="pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-row>
    <el-dialog title="编辑类型" :visible.sync="dialogVisible" width="50%">
      <el-form :model="formData">
        <el-form-item label="资源开放类型：" :label-width='itemWidth'>
          <el-radio-group v-model="formData.sourcePubType">
            <div><el-radio :label="1">加密（读者通过扫图书贴码并激活后才可查看资源，图书增值服务由人卫智数提供）</el-radio></div><br>
            <div style="margin-top:5px;"><el-radio :label="2">开放（读者微信扫码即可查看资源，无权限要求）</el-radio></div><br>
            <div style="margin-top:5px;"><el-radio :label="3">会员（读者通过微信扫码并且是人卫助手会员才可以查看资源）</el-radio></div>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialogue()">取 消</el-button>
        <el-button type="primary" @click="updatePubStatus()" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "bookmanage",
  props: {
    system: String,
  },
  data() {
    return {
      tableData: [],
      totalNum: 0,
      tableHeight: "",
      currentPage: 0, // 第几页
      pageSize: 10, // 每页显示的数据个数
      dialogVisible:false,
      itemWidth:'160px;',
      formData:{},
      multipleSelection:[],
      searchText: '',
      sourcePubType: '',
      pubStatus: '',
      formData: {},
    };
  },
  methods: {
    getTableData() {
      this.axios.get(CONST.SERVICE_REST_PACKAGE + "increaseBook/selectGetPageList", {
          params: {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            searchText: this.searchText,
            sourcePubType: this.sourcePubType,
            pubStatus: this.pubStatus,
          },
        })
        .then((response) => {
          this.totalNum = response.data.total;
          this.tableData = response.data.records;
        });
    },
    resetSearch(){
      this.searchText = ''
      this.sourcePubType = ''
      this.pubStatus = ''
    },
    openAddDialoge(){
      this.$router.push({
          name: 'bookAdd'
        }
      )
    },
    openEditDialoge(row){
      this.axios.get(CONST.SERVICE_REST_PACKAGE + "book/selectBookById", {
        params: {
          id: row.id
        },
      }).then((response) => {
        if(response.data){
          row = response.data
        }
      });
      this.$router.push({
          name: 'editIncreaseMetadata',
          params: {
            id: row.id,
            formData: row,
          }
        }
      )
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTableData();
    },
    openEditPubStatusDialoge(row){
      this.dialogVisible = true
      this.formData={id:row.id, sourcePubType:row.sourcePubType}
    },
    closeDialogue(){
      this.dialogVisible = false
      this.formData={}
    },
    updatePubStatus(){
      console.log(this.formData)
      let param = JSON.parse(JSON.stringify(this.formData))
      this.axios.post(CONST.SERVICE_REST_PACKAGE + "increaseBook/add", param)
        .then(() => {
          this.closeDialogue()
          this.getTableData()
          this.newUserDialogVisible = false
        })
    },
    deleteData(row) {
      this.$confirm('确认要删除该数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {id:row.id,pubStatus:3}
        this.axios.post(CONST.SERVICE_REST_PACKAGE + "increaseBook/update", param)
          .then(() => {
            this.getTableData();
          })
      }).catch(() => {

      });
    },
    formatterTime(row, column, cellValue, index){
      if(cellValue){
        var t=new Date(cellValue);//row 表示一行数据, updateTime 表示要格式化的字段名称
        let hour = this.dateIfAddZero(t.getHours());
        let minite = this.dateIfAddZero(t.getMinutes());
        return t.getFullYear()+"-"+(t.getMonth()+1)+"-"+t.getDate()+" "+hour+":"+minite
      }
      return "-";
    },
    dateIfAddZero : function (time) {
      return time < 10 ? '0'+ time : time
    },
    formatterPubType(row, column, cellValue, index){
      switch(cellValue){
        case 1:
          return "加密";
        case 2:
          return "开放";
        case 3:
          return "会员";
        default:
          return "-"
      }
    },
    formatterIsbn(row, column, cellValue, index){
      if(cellValue){
        return cellValue
      }else{
        return '-'
      }
    },
    formatterStatus(row){
      switch(row.pubStatus){
        case 1:
          return "新建";
        case 2:
          return "发布";
        default:
          return "新建"
      }
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    generateQrBook(row) {
      let param = {id:row.id,qualityStatus:2,pubStatus:2}
      this.axios.post(CONST.SERVICE_REST_PACKAGE + "increaseBook/update", param)
        .then(() => {
          this.getTableData();
        })
    },
    pubBook(row) {
      let param = {id:row.id,pubStatus:3}
      this.axios.post(CONST.SERVICE_REST_PACKAGE + "increaseBook/update", param)
        .then(() => {
          this.getTableData();
        })
    },
  },
  created() {
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 120;
    this.getTableData();
  },
};
</script>
<style scoped>
.el-form-item {
    margin-bottom: 12px;
}
.el-upload-list__item {
  transition: none !important;
}
.upload-template{
  display: inline-block;
}
</style>
